package echat.future.cn.echat.my.weight.dialog;


import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import echat.future.cn.echat.R;
import echat.future.cn.echat.my.weight.wheel.StringWheelView;

public class StringDialog extends Dialog{

    public StringDialog(@NonNull Context context) {
        this(context, R.style.dialogBottom);
    }

    public StringDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class  Builder{
        private Context mContext;
        private String mTitle;
        private OnComfirmListener mOnComfirmListener;
        private Dialog dialog;
        private List<String> mDatas = new ArrayList<>();

        public Builder datas(List<String> datas){
            this.mDatas.addAll(datas);
            return this;
        }
        public Builder(Context context){
            this.mContext = context;
        }
        public Builder title(String title){
            this.mTitle = title;
            return this;
        }
        public Builder comfirmListener(OnComfirmListener comfirmListener){
            this.mOnComfirmListener = comfirmListener;
            return this;
        }

        public Dialog builder(){
            View rootView = LayoutInflater.from(mContext).inflate(R.layout.dialog_string,null,false);
            StringWheelView stringWheelView = rootView.findViewById(R.id.stringWheel);
            rootView.findViewById(R.id.confirm).setOnClickListener(v->{if(mOnComfirmListener != null){
                dialog.dismiss();
                mOnComfirmListener.onConfirm(stringWheelView.getmSelectedText());

            }});
            rootView.findViewById(R.id.cancel).setOnClickListener(v -> dialog.dismiss());
            stringWheelView.setDataList(mDatas);
            ((TextView)rootView.findViewById(R.id.title)).setText(mTitle);
            dialog = new StringDialog(mContext);
            dialog.setContentView(rootView);


            Window window = dialog.getWindow();
            window.setGravity(Gravity.BOTTOM);

            WindowManager m = window.getWindowManager();
            Display d = m.getDefaultDisplay(); //为获取屏幕宽、高
            WindowManager.LayoutParams p = dialog.getWindow().getAttributes(); //获取对话框当前的参数值
            p.width = WindowManager.LayoutParams.MATCH_PARENT;
            dialog.getWindow().setAttributes(p); //设置生效
            dialog.setCancelable(false);
            return dialog;
        }

        public interface OnComfirmListener{
            void onConfirm(String title);
        }
    }
}
